package org.xue.snowmall.order.interceptor;

import entity.UserRecordEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author 小刘的小张
 * @version 1.0
 * @description: TODO
 * @date 2021/9/23 16:47
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserRecordEntity> loginUser = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {



        UserRecordEntity user = (UserRecordEntity) request.getSession().getAttribute("user");
        if (user != null) {
            //此时用户已经登录
            loginUser.set(user);
            return true;
        }
        //此时用户未登录重定向到登录界面进行登录
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<script>alert('请先进行登录，再进行后续操作！');location.href='http://auth.snow-mall.com?url=search.snow-mall.com'</script>");
        return false;
    }
}
